//
//  CertOID.m
//  TestProject
//
//  Created by 武侠 on 2022/2/17.
//  Copyright © 2022 zhulong. All rights reserved.
//

#import "CertOID.h"
#import "CertTLV.h"
#import "CertItemLength.h"

@implementation CertOID

+ (BOOL)isValid:(NSData *)data {
    if ([super isValid:data] == NO) {
        return NO;
    }
    char *sdata = (char *)data.bytes;
    int i = 0, end = (int)data.length;
    uint8_t type = sdata[i];
    
    while (type == 0x30 || type == 0x31) {
        CertItemLength *length = [[CertItemLength alloc] initWithData:sdata index:i+1];
        i += 1 + length.lenLen;
        if (i >= end) {
            return NO;
        }
        type = sdata[i];
    }
    
    return type == 0x06;
}

/*
 30 2F(47):
     06 08(08): 2B 06 01 05 05 07 02 01 (1.3.6.1.5.5.7.2.1)
                (http://www.cfca.com.cn/us/us-15.htm)
     16 23(35): 68 74 74 70 3A 2F 2F 77 77 77 2E 63 66 63 61 2E 63 6F 6D 2E 63 6E 2F 75 73 2F 75 73 2D 31 35 2E 68 74 6D
 */
- (void)loadCertData {
    char *data = (char *)self.data.bytes;
    int i = 0, end = (int)self.data.length;
    uint8_t type = data[i];
    
    while (type == 0x30 || type == 0x31) {
        CertItemLength *length = [[CertItemLength alloc] initWithData:data index:i+1];
        i += 1 + length.lenLen;
        type = data[i];
    }

    
    // 解析：oid 06
    // 计算oid的长度信息
    CertItemLength *length = [[CertItemLength alloc] initWithData:data index:i+1];
    self.oid = [CertTLV value:data begin:i];
    self.oidDesc = [CertOID oid:self.oid];
    
    // 偏移oid的长度
    i += 1 + length.lenLen + length.len;
    if (i >= end) {
        return;
    }
    
    // 解析：value
    [self loadOIDValue:data begin:i end:end];
}

- (void)loadOIDValue:(char *)data begin:(int)begin end:(int)end {
    self.value = [CertTLV value:data begin:begin];
}


+ (NSString *)oid: (NSString *)key {
    static NSDictionary<NSString *, NSString *> *oid;
    if (oid == NULL) {
        oid = @{
            @"0.9.2342.19200300.100.1.1" : @"UID",
            @"0.9.2342.19200300.100.1.2" : @"textEncodedORAddress",
            @"0.9.2342.19200300.100.1.3" : @"mail",
            @"0.9.2342.19200300.100.1.4" : @"info",
            @"0.9.2342.19200300.100.1.5" : @"favouriteDrink",
            @"0.9.2342.19200300.100.1.6" : @"roomNumber",
            @"0.9.2342.19200300.100.1.7" : @"photo",
            @"0.9.2342.19200300.100.1.8" : @"userClass",
            @"0.9.2342.19200300.100.1.9" : @"host",
            
            @"1.2.840.10040.4.1"    : @"DSA",
            @"1.2.840.10040.4.3"    : @"sha1DSA",
            @"1.2.840.10045.2.1"    : @"ECC",
            @"1.2.156.10197.1.100"  : @"块密码",
            @"1.2.156.10197.1.102"  : @"SM1块密码",
            @"1.2.156.10197.1.103"  : @"SSF33块密码",
            @"1.2.156.10197.1.104"  : @"SM4块密码",
            @"1.2.156.10197.1.200"  : @"流密码",
            @"1.2.156.10197.1.201"  : @"ZUC流密码",
            @"1.2.156.10197.1.300"  : @"公钥密码",
            @"1.2.156.10197.1.301"  : @"SM2",
            @"1.2.156.10197.1.302"  : @"SM9",
            @"1.2.156.10197.1.400"  : @"SM3",
            @"1.2.156.10197.1.401"  : @"SM3 Hash算法",
            @"1.2.156.10197.1.500"  : @"摘要签名",
            @"1.2.156.10197.1.501"  : @"SM3的SM2签名",
            @"1.2.156.10197.1.502"  : @"SHA1的SM2签名",
            @"1.2.156.10197.1.503"  : @"SHA256的SM2签名",
            @"1.2.156.10197.1.504"  : @"SM3的RSA签名",
            @"1.2.156.10197.4.3"    : @"证书颁发机构",
            @"1.2.156.10197.6"      : @"标准类",
            @"1.2.156.10197.6.1"    : @"Fundatation类",
            @"1.2.156.10197.6.1.1"  : @"算法类",
            @"1.2.156.10197.6.1.2"  : @"ID类",
            @"1.2.156.10197.6.1.3"  : @"运行模式",
            @"1.2.156.10197.6.1.4"  : @"安全机制",
            @"1.2.156.10197.6.2"    : @"设备类",
            @"1.2.156.10197.6.3"    : @"服务类",
            @"1.2.156.10197.6.4"    : @"基础设施",
            @"1.2.156.10197.6.5"    : @"测试类",
            @"1.2.156.10197.6.5.1"  : @"随机测试类",
            @"1.2.156.10197.6.6"    : @"管理类",
            
            @"1.2.840.113549.1.1.1" : @"RSA",
            @"1.2.840.113549.1.1.2" : @"md2的RSA签名",
            @"1.2.840.113549.1.1.3" : @"md4的RSA签名",
            @"1.2.840.113549.1.1.4" : @"md5的RSA签名",
            @"1.2.840.113549.1.1.5" : @"sha1的RSA签名",
            @"1.2.840.113549.1.1.11": @"sha256的RSA签名",
            @"1.2.840.113549.1.1.13": @"sha512的RSA签名",
            @"1.2.840.113549.1.9.1" : @"email",
            
            @"1.3.6.1.5.5.7.2.1"    : @"CPS",
            @"1.3.6.1.5.5.7.3.1"    : @"服务器身份验证",
            @"1.3.6.1.5.5.7.3.2"    : @"客户端身份验证",
            @"1.3.6.1.5.5.7.3.3"    : @"代码签名",
            @"1.3.6.1.5.5.7.3.4"    : @"电子邮件保护",
            @"1.3.6.1.5.5.7.3.8"    : @"时间戳",
            @"1.3.6.1.5.5.7.1.1"    : @"颁发机构信息访问",
            @"1.3.6.1.5.5.7.48.1"   : @"OCSP",
            @"1.3.6.1.5.5.7.48.2"   : @"CA Issuers",
            @"1.3.6.1.4.1.311.2.1.21"   : @"msCodeInd",
            @"1.3.6.1.4.1.311.2.1.22"   : @"msCodeCom",
            @"1.3.6.1.4.1.311.10.3.1"   : @"msCTLSign",
            @"1.3.6.1.4.1.311.10.3.3"   : @"msSGC",
            @"1.3.6.1.4.1.311.10.3.4"   : @"msEFS",
            @"1.3.6.1.4.1.311.60.2.1.1" : @"所在地",
            @"1.3.6.1.4.1.311.60.2.1.3" : @"国家",
            @"1.3.6.1.4.1.311.60.2.1.2" : @"州或省",
            @"1.3.14.3.2.29"        : @"sha1的RSA签名-2",
            @"2.16.840.1.113730.1.1": @"Netscape证书类型",
            
            @"2.5.4.3"              : @"CN",
            @"2.5.4.32"             : @"owner",
            @"2.5.4.4"              : @"SN",
            @"2.5.4.42"             : @"GN",
            @"2.5.4.43"             : @"initials",
            @"2.5.4.5"              : @"SN",
            @"2.5.4.6"              : @"C",
            @"2.5.4.7"              : @"L",
            @"2.5.4.8"              : @"ST",
            @"2.5.4.9"              : @"颁发者街区地址",
            @"2.5.4.10"             : @"O",
            @"2.5.4.11"             : @"OU",
            @"2.5.4.12"             : @"颁发者标题",
            @"2.5.4.13"             : @"颁发者描述",
            @"2.5.4.15"             : @"颁发者业务类别",
            @"2.5.4.20"             : @"phone",
            @"2.5.4.45"             : @"UID",
            @"2.5.29.14"            : @"主体密钥标识符",
            @"2.5.29.15"            : @"使用密钥",
            @"2.5.29.17"            : @"使用者备用名称",
            @"2.5.29.18"            : @"颁发者备用名称",
            @"2.5.29.19"            : @"基本限制",
            @"2.5.29.31"            : @"CRL分发点",
            @"2.5.29.32"            : @"CP 证书策略",
            @"2.5.29.35"            : @"机构密钥标识符",
            @"2.5.29.37"            : @"扩展密钥用法",
            @"2.5.29.46"            : @"最新的CRL",
            @"2.23.140.1.2.2"       : @"组织验证",
            
            /* 北京CA扩展项 */
            @"1.2.156.112562.2.1.1.1"  : @"BJCA_机构企业代码",
            @"2.16.840.1.113732.2"     : @"BJCA_个人身份证号",
            @"1.2.86.840.1.176.1"      : @"BJCA_未知扩展1",
            @"1.2.156.112562.2.1.1.14" : @"BJCA_未知扩展14",
            @"1.2.156.112562.2.1.1.30" : @"BJCA_未知扩展30",
            @"1.2.156.112562.6.3.2"    : @"BJCA_未知扩展3.2",
            
            /* 自定义扩展项 */
            @"1.2.156.10260.4.1.1"     : @"个人身份标识码",
            @"1.2.156.10260.4.1.2"     : @"个人社会保险号码",
            @"1.2.156.10260.4.1.3"     : @"企业工商注册号码",
            @"1.2.156.10260.4.1.4"     : @"企业组织机构代码",
            @"1.2.156.10260.4.1.5"     : @"企业税号",
            
            @"1.2.156.112562.2.1.1.23" : @"证书实体唯一标识",
        };
    }
    return oid[key] ? oid[key] : key;
}


- (NSString *)description {
    return [NSString stringWithFormat:@"%@(%@) = %@", self.oid, self.oidDesc, self.value];
}

@end
